package com.qfedu.buy.server.service.impl;

import com.qfedu.buy.common.config.RedisKeyConfig;
import com.qfedu.buy.common.dto.SkillGoodsDto;
import com.qfedu.buy.common.util.RedissonUtil;
import com.qfedu.buy.common.vo.R;
import com.qfedu.buy.server.dao.SkillGoodsDao;
import com.qfedu.buy.server.service.intf.SkillActivityService;
import com.qfedu.buy.server.service.intf.SkillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: NeighborBuy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-02-05 09:41
 */
@Service
public class SkillGoodsServiceImpl implements SkillGoodsService {
    @Autowired
    private SkillGoodsDao dao;

    @Override
    public R queryGoodsV1(int said) {
        return R.ok(dao.selectBySaid(said));
    }
    //缓存
    @Override
    public R queryGoodsV2(int said) {
        if(said>0) {
            if (RedissonUtil.isHave(RedisKeyConfig.SKILL_ACTIVITY_GOODS + said)) {
                return R.ok(RedissonUtil.getList(RedisKeyConfig.SKILL_ACTIVITY_GOODS + said));
            } else {
                //查询数据库
                List<SkillGoodsDto> list = dao.selectBySaid(said);
                //同步数据
                RedissonUtil.setList(RedisKeyConfig.SKILL_ACTIVITY_GOODS + said, list);
                RedissonUtil.expire(RedisKeyConfig.SKILL_ACTIVITY_GOODS + said, RedisKeyConfig.SKILL_ACTIVITY_GOODS_TIME);
                return R.ok(list);
            }
        }
        return R.fail();
    }
}
